package com.sdjzu.wanfuying.androiddesign;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewTreeObserver;

import com.sdjzu.wanfuying.androiddesign.base.BaseActivity;

public class TouchDrawActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_touch_draw);
    }

    public static class TouchDrawView extends View {

        private Paint mPaint;
        private Path mPath;
        private Bitmap mBitmap;
        private Canvas mCanvas;
        private float currentX;
        private float currentY;
        private VelocityTracker mVelocityTracker;

        public TouchDrawView(Context context) {
            super(context);
        }

        public TouchDrawView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }

        public TouchDrawView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        public TouchDrawView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
        }

        @Override
        public boolean dispatchTouchEvent(MotionEvent event) {
            return super.dispatchTouchEvent(event);
        }

        @Override
        protected void onFinishInflate() {
            super.onFinishInflate();
            this.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    init(getMeasuredWidth(), getMeasuredHeight());
                    mVelocityTracker = VelocityTracker.obtain();
                }
            });
        }

        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawBitmap(mBitmap, 0, 0, mPaint);
            // canvas.drawPath(mPath, mPaint);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if (null != mVelocityTracker)
                mVelocityTracker.addMovement(event);
            float x = event.getX();
            float y = event.getY();

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    currentX = x;
                    currentY = y;
                    // mPath.moveTo(currentX, currentY);
                    mCanvas.drawPoint(currentX, currentY, mPaint);
                    break;
                case MotionEvent.ACTION_MOVE:
                    mVelocityTracker.computeCurrentVelocity(1);
                    double speed = Math.sqrt(Math.pow(mVelocityTracker.getXVelocity(), 2) + Math.pow(mVelocityTracker.getYVelocity(), 2));
                    float size = (float) (speed + 5);
                    mPaint.setStrokeWidth(size);
                    // mPath.quadTo(currentX, currentY, x, y); // 画线
                    mCanvas.drawLine(currentX, currentY, x, y, mPaint);
                    currentX = x;
                    currentY = y;
                    break;
                case MotionEvent.ACTION_UP:
                    // mCanvas.drawPath(mPath, mPaint);
                    // mCanvas.drawPoint(currentX, currentY, mPaint);
                    break;
            }

            invalidate();
            return true;
        }

        private void init(int screenWidth, int screenHeight) {
            mPaint = new Paint();
            mPaint.setAntiAlias(true); // 去除锯齿
            mPaint.setStrokeWidth(5);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setColor(Color.BLACK);

            mPath = new Path();

            mBitmap = Bitmap.createBitmap(screenWidth, screenHeight, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);
            // mCanvas.drawColor(Color.WHITE);
        }
    }
}
